<?php

    class Objects_model extends MY_Model{

        function __construct()
        {
            parent::__construct();
            $this->table = 'objects';
        }

        function build_form($object_type,$group_ID)
        {

            switch ($this->input->post('id')) {
                case TRUE:
                    $data['post_action'] = "EDIT";
                    //get object row
                    $data['object'] = $this->objects_model->view_edit($_POST['id']);
                    $data['fields'] = unserialize($data['object']['fields']);
                    break;
                case FALSE:
                    $data['post_action'] = "NEW";
                    break;
            }

            $q = 'SELECT oa.ID, oa.type, oa.custom, a.ID as att_ID, a.name, a.value, a.required, a.custom
            FROM object_attributes as oa
            LEFT JOIN attributes as a ON oa.attributes_ID = a.ID
            WHERE oa.type = "'.$object_type.'"
            AND oa.groups_ID = "'.$group_ID.'"
            ORDER BY oa.order ASC, oa.custom ASC, oa.ID ASC
            ';

            $attributes = $this->db->query($q)->result_array();
            //$this->firephp->log($this->db->last_query());
            $data['counter'] = 0;
            $html = '';
            foreach ($attributes as $attribute):
            $data['attribute'] = $attribute;

            $data['counter']++;

            switch ($attribute['value']) {
                case 'descriptive data':
                    $html .= $this->load->view('objects/forms/elem_input', $data, TRUE); 
                    break;
                case 'yes/no':
                    $html .= $this->load->view('objects/forms/elem_checkbox', $data, TRUE); 
                    break;
            }

            endforeach;

            //$this->firephp->log($this->data['attributes']);
            return $html;
        }

        function view_edit($object_ID)
        {
            $q = '
            SELECT oo.*, o.ID as ownerid, o.name as oname, o.surname as osurname
            FROM objects as oo
            LEFT JOIN owners as o ON oo.owners_ID = o.ID
            WHERE oo.ID = "'.$object_ID.'"
            ';

            $res = $this->db->query($q)->row_array();
            return $res;

        }
        
        function view_all(){

            $q = '
            SELECT oo.*, o.ID as ownerid, o.name as oname, o.surname as osurname
            FROM objects as oo
            LEFT JOIN owners as o ON oo.owners_ID = o.ID
            ';

            $res = $this->db->query($q)->result_array();
            return $res;

        }

        function validate_custom($object_type)
        {
            /*Custom required fields*/
            $q = 'SELECT oa.ID, oa.type, oa.custom, a.ID as att_ID, a.name, a.value, a.required, a.custom
            FROM object_attributes as oa
            LEFT JOIN attributes as a ON oa.attributes_ID = a.ID
            WHERE oa.type = "'.$object_type.'" AND oa.custom = 1 AND a.required = 1
            ORDER BY oa.order ASC, oa.custom ASC, oa.ID ASC 
            '; 

            $res = $this->db->query($q)->result_array();  
            return $res; 
        }
        
        function delete($post_ID)
        {
            //Delete post
            $this->db->where('ID',$post_ID);
            $this->db->delete('objects');

            if($this->db->affected_rows()>=0){
                return TRUE;
            }else{
                return TRUE;
            }
        }
        
        function get_groups($object_TYPE)
        {
            
        }
    }
?>
